package com.easy.auth.service.impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;

import com.easy.auth.domain.Role;
import com.easy.auth.service.RoleService;
import com.easy.base.service.impl.EManagerImpl;
import com.easy.common.jdbc.DymaticCondition;
import com.easy.common.jdbc.PageInfo;
import com.easy.common.util.BeanUtil;
import com.easy.common.web.PagedList;

@Service
public class RoleServiceImpl extends EManagerImpl<Role> implements RoleService {

	@Override
	public PagedList<Role> getAllRoles(Role role, PageInfo pageInfo) {
		return super.findByDynamicFilterCondition(role, pageInfo);
	}

	@Override
	public Role findRole(Role role) {
		return super.find(role);
	}

	@Override
	public int saveOrEditRole(Role role) {
		return super.saveOrEdit(role);
	}

	@Override
	public int removeRoles(String oids) {
		int count = 0;
		String[] userIds = oids.split("-");
		String userRoleDeletesql = "DELETE FROM userinfo_role WHERE roleid = ?";
		String rolActionDeletesql = "DELETE FROM role_action WHERE roleid = ?";
		if (userIds != null) {
			for (String oid : userIds) {
				super.update(userRoleDeletesql, oid);
				super.update(rolActionDeletesql, oid);
				Role role = new Role();
				role.setOid(oid);
				count += super.remove(role);
			}
		}

		return count;
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagedList<Role> getRolesByUserId(String userid, PageInfo pageInfo) {
		String sql = "SELECT role.* FROM role, userinfo_role uro WHERE 1=1 and role.oid = uro.roleid";
		DymaticCondition dymaticCondition = new DymaticCondition();
		dymaticCondition.addCondition("AND uro.userid = '?'", userid);

		return super.query(
				sql,
				dymaticCondition,
				new BeanPropertyRowMapper<Role>(BeanUtil
						.getGenericType(this, 0)), pageInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagedList<Role> getNotHaveRolesByUserId(String userid,
			PageInfo pageInfo) {
		String sql = "SELECT * FROM role WHERE 1= 1";
		DymaticCondition dymaticCondition = new DymaticCondition();
		dymaticCondition
				.addCondition(
						"AND oid NOT IN(SELECT role.oid FROM role,userinfo_role uro WHERE uro.roleId = role.oid and uro.userid = '?')",
						userid);
		return super.query(
				sql,
				dymaticCondition,
				new BeanPropertyRowMapper<Role>(BeanUtil
						.getGenericType(this, 0)), pageInfo);
	}

	public int removeRoles(String userid, String[] roleids) {
		int count = 0;
		if (roleids != null) {
			for (String roleid : roleids) {
				String sql = "DELETE FROM userinfo_role WHERE userid = ? and roleid = ?";
				count += super.update(sql, userid, roleid);
			}
		}

		return count;
	}

	public int addRoles(String userid, String[] roleids) {
		int count = 0;
		if (roleids != null) {
			for (String roleid : roleids) {
				String sql = "INSERT INTO userinfo_role VALUES(?, ?)";
				count += super.update(sql, userid, roleid);
			}
		}

		return count;
	}

}
